﻿namespace QFlow.Windows.Forms
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.Linq;
    using System.Drawing;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Windows.Forms;
    using QFlow.Data;

    /// <summary>
    /// Brands List Form
    /// </summary>
    public partial class BrandsList : QFlow.Utils.BaseForm
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="BrandsList"/> class.
        /// </summary>
        public BrandsList(QFlowDbContext context)
            : base(context)
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Fills the brands grid
        /// </summary>
        private void FillBrands()
        {
            this.basicBrandsBindingSource.DataSource = this.Context.ProductBrands;
        }

        /// <summary>
        /// Handles the Load event of the BrandsList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BrandsList_Load(object sender, EventArgs e)
        {
            this.FillBrands();
        }

        /// <summary>
        /// Handles the OptionsClick event of the warning control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Warning_OptionsClick(object sender, EventArgs e)
        {
            if (this.Validate())
            {
                try
                {
                    this.Context.SubmitChanges();
                    this.warning.Hide();
                }
                catch
                {
                    MessageBox.Show(QFlow.Windows.Properties.Resources.MBCantSaveChanges, QFlow.Windows.Properties.Resources.MBErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    ResetContext();
                }
            }
        }

        /// <summary>
        /// Handles the CellEndEdit event of the gvBrands control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DataGridViewCellEventArgs"/> instance containing the event data.</param>
        private void GvBrands_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            this.warning.Show();
        }

        /// <summary>
        /// Handles the RowValidating event of the gvBrands control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DataGridViewCellCancelEventArgs"/> instance containing the event data.</param>
        private void GvBrands_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            var cell = this.gvBrands[e.ColumnIndex, e.RowIndex];

            if (!gvBrands.Rows[e.RowIndex].IsNewRow)
            {
                if (cell.Value == null || string.IsNullOrEmpty(cell.Value.ToString()))
                {
                    cell.ErrorText = QFlow.Windows.Properties.Resources.GVEmptyValue;
                    e.Cancel = true;
                }
                else if (cell.Value.ToString().Length >= 50)
                {
                    cell.ErrorText = QFlow.Windows.Properties.Resources.GVLongValue;
                    e.Cancel = true;
                }
                else
                {
                    cell.ErrorText = string.Empty;
                    e.Cancel = false;
                }
            }
        }

        /// <summary>
        /// Handles the UserDeletedRow event of the gvBrands control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DataGridViewRowEventArgs"/> instance containing the event data.</param>
        private void GvBrands_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
        {
            this.warning.Show();
        }

        /// <summary>
        /// Handles the FormClosing event of the BrandsList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void BrandsList_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (gvBrands.CausesValidation)
            {
                gvBrands.CausesValidation = false;
                this.Close();
            }
        }
    }
}